<?php

namespace App\Console\Commands\OpenShop;

use App\Library\Rms\RmsUserApi;
use App\Models\OpenShop\Common\OpenShopOrderManagerModel;
use Illuminate\Console\Command;

class RefreshManagerName extends Command
{
    /**
     * 更新经理人姓名
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'openShop:refresh-manage-name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新经理人姓名';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // OpenShopOrderManagerModel update
        $userIds = OpenShopOrderManagerModel::query()->groupBy('managerUid')->pluck('managerUid')->toArray();

        collect((new RmsUserApi())->getUserInfoByIds($userIds, ['id', 'username'])['data'])->each(function ($item) {

            $this->info($item['id'] . '-' . $item['username']);

            OpenShopOrderManagerModel::query()
                                     ->where('managerUid', $item['id'])
                                     ->update(['managerName' => $item['username']]);
        });

        // TODO other table
    }
}
